package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.PmsProductMapper;
import com.itheima.pojo.PmsProduct;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@DubboService
public class PmsProductImpl implements PmsProductApi {


    @Autowired
    private PmsProductMapper mapper;

    /**
     * 查询商品
     */
    @Override
    public IPage<PmsProduct> list(Integer pageNum, Integer pageSize, Integer brandId, String keyword, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {

        IPage<PmsProduct> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(brandId), PmsProduct::getBrandId, brandId)
                .eq(ObjectUtils.isNotEmpty(productCategoryId), PmsProduct::getProductCategoryId, productCategoryId)
                .eq(ObjectUtils.isNotEmpty(publishStatus), PmsProduct::getPublishStatus, publishStatus)
                .eq(ObjectUtils.isNotEmpty(verifyStatus), PmsProduct::getVerifyStatus, verifyStatus)
                .eq(PmsProduct::getDeleteStatus, 0)
                .like(ObjectUtils.isNotEmpty(keyword), PmsProduct::getName, keyword)
                .like(ObjectUtils.isNotEmpty(productSn), PmsProduct::getProductSn, productSn);

        mapper.selectPage(page, lqw);

        return page;

    }

    /**
     * 根据商品名称或货号模糊查询
     */
    @Override
    public List<PmsProduct> simpleList(String keyword) {

        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(ObjectUtils.isNotEmpty(keyword), PmsProduct::getKeywords, keyword);
        List<PmsProduct> list = mapper.selectList(lqw);
        return list;
    }

    /**
     * 批量修改删除状态
     */
    @Override
    public void deleteStatus(Integer deleteStatus, List<Integer> ids) {
        List<PmsProduct> list = mapper.selectBatchIds(ids);
        for (PmsProduct pmsProduct : list) {
            pmsProduct.setDeleteStatus(deleteStatus);
            mapper.updateById(pmsProduct);
        }
    }

    /**
     * 批量设为新品
     */
    @Override
    public Integer newStatus(Integer newStatus, List<Integer> ids) {

        int flag = 0;

        List<PmsProduct> list = mapper.selectBatchIds(ids);

        for (PmsProduct pmsProduct : list) {
            if (pmsProduct.getVerifyStatus() == 0) {
                flag = 1;
                return flag;
            }

            if (pmsProduct.getPublishStatus() == 0){
                flag = 2;
                return flag;
            }
        }

        for (PmsProduct pmsProduct : list) {
            pmsProduct.setNewStatus(newStatus);
            mapper.updateById(pmsProduct);
        }

        return flag;
    }

    /**
     * 批量上下架商品
     */
    @Override
    public Integer publishStatus(Integer publishStatus, List<Integer> ids) {

        int flag = 0;

        List<PmsProduct> list = mapper.selectBatchIds(ids);

        for (PmsProduct pmsProduct : list) {
            if (pmsProduct.getVerifyStatus() == 0 && publishStatus == 1) {
                flag = 1;
                return flag;
            }
        }

        for (PmsProduct pmsProduct : list) {

            pmsProduct.setPublishStatus(publishStatus);

            if (publishStatus == 0){
                pmsProduct.setNewStatus(0);
                pmsProduct.setRecommandStatus(0);
            }

            mapper.updateById(pmsProduct);
        }

        return flag;
    }

    /**
     * 批量推荐商品
     */
    @Override
    public Integer recommendStatus(Integer recommendStatus, List<Integer> ids) {

        int flag = 0;

        List<PmsProduct> list = mapper.selectBatchIds(ids);

        for (PmsProduct pmsProduct : list) {
            if (pmsProduct.getVerifyStatus() == 0) {
                flag = 1;
                return flag;
            }

            if (pmsProduct.getPublishStatus() == 0){
                flag = 2;
                return flag;
            }
        }

        for (PmsProduct pmsProduct : list) {
            pmsProduct.setRecommandStatus(recommendStatus);
            mapper.updateById(pmsProduct);
        }

        return flag;
    }

    /**
     * 批量修改审核状态
     */
    @Override
    public void verifyStatus(Integer verifyStatus, List<Integer> ids) {
        List<PmsProduct> list = mapper.selectBatchIds(ids);
        for (PmsProduct pmsProduct : list) {
            pmsProduct.setVerifyStatus(verifyStatus);
            mapper.updateById(pmsProduct);
        }
    }


    /**
     * 根据品牌的id 查询商品
     *
     * @param id
     * @return
     */
    @Override
    public List<PmsProduct> findByBrandId(Integer id) {
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProduct::getBrandId, id);
        List<PmsProduct> pmsProducts = mapper.selectList(wrapper);
        return pmsProducts;
    }

    /**
     * 创建商品
     */
    @Override
    public PmsProduct save(PmsProduct pmsProduct) {

        List<PmsProduct> productList = findAll();

        for (PmsProduct product : productList) {
            if (StringUtils.pathEquals(pmsProduct.getName(), product.getName())) {
                return null;
            }
        }

        mapper.insert(pmsProduct);
        return pmsProduct;
    }

    @Override
    public List<PmsProduct> findAll() {
        List<PmsProduct> pmsProducts = mapper.selectList(new LambdaQueryWrapper<>());
        return pmsProducts;
    }

    /**
     * 根据商品id获取商品编辑信息
     */
    @Override
    public PmsProduct updateInfo(Integer productId) {
        PmsProduct pmsProduct = mapper.selectById(productId);
        return pmsProduct;
    }

    /**
     * 更新商品
     */
    @Override
    public boolean updateProduct(PmsProduct pmsProduct) {

        boolean flag = false;

        List<PmsProduct> productList = findAll();

        for (PmsProduct product : productList) {
            if (StringUtils.pathEquals(pmsProduct.getName(), product.getName()) && !Objects.equals(pmsProduct.getId(), product.getId())) {
                flag = true;
                return flag;
            }
        }
        mapper.updateById(pmsProduct);
        return flag;
    }


}
